/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vzone.market;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.UUID;
import vzone.bank.Bank;
import vzone.community.Community;
import vzone.sql.SqlHelper;
import vzone.structs.Billing;
import vzone.structs.Company;
import vzone.structs.Goods;
import vzone.structs.OperatorCode;

/**
 *
 * @author Kingpro
 */
public class Shop implements IShop
{

    private Company company;
    private static final String SELECTCOMPANY = "SELECT TOP 1 * FROM shop WHERE `idShop`=@shop";
    private static final String SELECTGOODS = "SELECT * FROM goods WHERE `idCompany`=@Company;";
    private static final String EXCHANGERECORD = "INSERT INTO exchange (`idExchange`, `idShop`, `idAccount`, `idGoods`, `timeExchange`, `unitSale`, `countGoods`, `sumupGoods`) VALUES(@id1, @id2, @id3, @id4, @time, @unit, @count, @sumup);";
    private static final String IP = "localhost";
    private static final int PORT = 3306;
    private static final String DB = "market";
    private static final String USER = "root";
    private static final String PWD = "lavender";

    public Shop(UUID companyID) throws Exception
    {
	company = new Company();

	ArrayList companysAl = SqlHelper.ExecuteReader(SqlHelper.getConnect(IP, PORT, DB, USER, PWD), SELECTCOMPANY, new Object[]
		{
		    companyID
		});
	Object[] resultObjects = (Object[]) companysAl.iterator().next();
	company.IdUuid = companyID;
	company.Owner = Community.getCommunity().CheckPeopleInfo(UUID.fromString((String) resultObjects[1]));
	company.NameS = (String) resultObjects[2];
	company.StartD = (Date) resultObjects[3];
	company.DescriptionS = (String) resultObjects[4];
	company.pwdHash = (String) resultObjects[5];
    }

    @Override
    public ArrayList<Goods> ListAll()
    {
	//SQL语句
	String cmdS = SELECTGOODS;
	try
	{
	    //执行SQL
	    ArrayList resultAl = SqlHelper.ExecuteReader(SqlHelper.getConnect(IP, PORT, DB, USER, PWD), cmdS, new Object[]
		    {
			company.IdUuid
		    });
	    ArrayList<Goods> goods = new ArrayList<Goods>();
	    //获取Iterator
	    Iterator<Object> resultI = resultAl.iterator();
	    //类型转换
	    while (resultI.hasNext())
	    {
		Object[] resultOs = (Object[]) resultI.next();
		Goods good = new Goods();
		good.CompanyIdUuid = company.IdUuid;
		good.IdUuid = UUID.nameUUIDFromBytes((byte[]) resultOs[0]);
		good.NameS = (String) resultOs[1];
		good.SaleMoneyD = Double.parseDouble((String) resultOs[2]);
		//good.CostsD = Double.parseDouble((String) resultOs[4]);
		good.CountI = Integer.parseInt((String) resultOs[3]);

		goods.add(good);
	    }
	    return goods;
	}
	catch (Exception ex)
	{
	    return null;
	}
    }

    @Override
    public OperatorCode BuyObjects(ArrayList<Goods> objsL, UUID BillingID)
    {
	Iterator<Goods> goods = objsL.iterator();
	double sumupD = 0;
	while (goods.hasNext())
	{
	    Goods good = goods.next();
	    sumupD += good.SaleMoneyD * good.CountI;
	}
	Billing billing = new Billing();
	OperatorCode code = Bank.getBank().CheckInBilling(BillingID, company.Owner.accountIdUuid, company.pwdHash, billing);
	if (code == OperatorCode.SUCCESS)
	{
	    if (billing.Balance == sumupD)
	    {
		//执行记录操作
		Iterator<Goods> goodst = objsL.iterator();
		while (goodst.hasNext())
		{
		    Goods good = goodst.next();
		    try
		    {
			SqlHelper.ExecuteNonQuery(SqlHelper.getConnect(IP, PORT, DB, USER, PWD), EXCHANGERECORD, new Object[]
				{
				    UUID.randomUUID(), company.IdUuid, billing.FromAccount, good.IdUuid, new Date(), good.SaleMoneyD, good.CountI, good.SaleMoneyD * good.CountI
				});
		    }
		    catch (Exception ex)
		    {
		    }
		}
		return code;
	    }
	    else
	    {
		Bank.getBank().Transfer(billing.Balance, company.Owner.accountIdUuid, company.pwdHash, billing.FromAccount);
		return OperatorCode.NOFITBILLING;
	    }
	}
	else
	{
	    return code;
	}
    }

    @Override
    public Company GetBusinessInfo()
    {
	return company;
    }
}
